﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace uz.control
{
    public class UzTreeNodeToolBar
    {
        public List<UzTreeNodeTool> Tools = new List<UzTreeNodeTool>();

        public delegate void HandleRunCommand(string CommandName);
        public event HandleRunCommand RunCommand;

        public void OnRunCommand(string CommandName)
        {
            if (RunCommand != null)
            {
                RunCommand(CommandName);
            }
        }

        #region 父节点：Parent
        /// <summary>
        /// 父节点
        /// </summary>
        private UzTreeNode _Parent = null;
        /// <summary>
        /// 父节点
        /// </summary>
        public UzTreeNode Parent
        {
            get { return _Parent; }
            set { _Parent = value; }
        }
        #endregion

        public UzTreeNodeToolBar(UzTreeNode Parent)
        {
            this.Parent = Parent;
        }

        private RectangleF _Bounds = new Rectangle();

        public RectangleF Bounds
        {
            get { return _Bounds; }
            set { _Bounds = value; }
        }

        public void AddCommand(string name, string text)
        {
            Tools.Add(new UzTreeNodeTool() {Name=name,DisplayText = text });
        }

        public void DrawToolBar(PaintEventArgs e,Rectangle Bounds,Font font)
        {
            SizeF sf = SizeF.Empty;
            string strText= "";

            _Bounds.Y = Bounds.Y;
            _Bounds.Height = Bounds.Height;
            _Bounds.X = Bounds.Right;
            _Bounds.Width = 0;
            

            for (int i = Tools.Count-1; i >=0; i--)
            {
                strText = Tools[i].DisplayText;
                sf = e.Graphics.MeasureString(strText, font);
                _Bounds.Width += sf.Width + 4;
                _Bounds.X -= sf.Width + 4;

                Tools[i].Bounds.X = (int)_Bounds.X;
                Tools[i].Bounds.Y = (int)(_Bounds.Y +2);
                Tools[i].Bounds.Width = (int)sf.Width + 1;
                Tools[i].Bounds.Height = (int)sf.Height;

                Point pt = Parent.UzTreeView.CurrentMouseMovePoint;

                if (Tools[i].Bounds.Contains(pt) == true)
                {
                    e.Graphics.FillRectangle(Brushes.RoyalBlue, Tools[i].Bounds);
                    e.Graphics.DrawRectangle(Pens.Black, Tools[i].Bounds);
                    e.Graphics.DrawString(strText, font, Brushes.WhiteSmoke, Tools[i].Bounds.X + 2, Tools[i].Bounds.Y + 2);
                }
                else
                {
                    e.Graphics.DrawString(strText, font, Brushes.Black, Tools[i].Bounds.X + 2, Tools[i].Bounds.Y + 2);

                }
            }
        }

        public bool RunCommandAtPoint()
        {
            bool rst = false;

            Point pt = Parent.UzTreeView.CurrentMouseMovePoint;

            foreach (UzTreeNodeTool itm in Tools)
            {
                if (itm.Bounds.Contains(pt) == true)
                {
                    OnRunCommand(itm.Name);
                    rst = true;
                    break;
                }
            }

            return rst;
        }

    }
}

